package com.woniuxy.bookmall.controller;

import com.woniuxy.bookmall.entity.TArea;
import com.woniuxy.bookmall.entity.TCity;
import com.woniuxy.bookmall.entity.TProvince;
import com.woniuxy.bookmall.entity.TUser;
import com.woniuxy.bookmall.service.AddressService;
import com.woniuxy.bookmall.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class AddressController {

    @Autowired
    AddressService addressService ;

    @RequestMapping("/adderss/getAllProvince")
    @ResponseBody
    public List<TProvince> getAllProvince(){
        List<TProvince> provinceList = addressService.getAllProvince();
        return provinceList ;
    }

    @RequestMapping("/adderss/getCityByProvince")
    @ResponseBody
    public List<TCity> getCityByProvince(String code){
        List<TCity> cityList = addressService.getCityByProvince(code);
        return cityList ;
    }

    @RequestMapping("/adderss/getAreaByCity")
    @ResponseBody
    public List<TArea> getAreaByCity(String code){
        List<TArea> areaList = addressService.getAreaByCity(code);
        return areaList ;
    }

    @RequestMapping("/adderss/addAddress")
    @ResponseBody
    public String addAddress(AddressVo addressVo, HttpServletRequest request){
        try {
            TUser user = (TUser) request.getSession().getAttribute("userInfo");
            addressService.add(addressVo,user);
            return "1";
        }catch (Exception e) {
            e.printStackTrace();
            return "0" ;
        }
    }

}
